<template>
  <div class="AddOrUpdate_main add_container tenantMenu_main main_container user_container">
    <div v-loading="loading" class="main_main">
      <div class="main_main_container">
        <div class="main_main_left">
          <div class="title" />
          <div class="titleText">部门</div>
          <div class="main_main_left_top">
            <el-tree
              ref="tree"
              :data="deptData"
              node-key="id"
              :expand-on-click-node="false"
              :default-expand-all="true"
              :highlight-current="true"
              @node-click="treeNodeClick"
            />
          </div>
        </div>
        <div class="main_main_right">
          <div style="padding-right:20px" class="SearchBar_main main_main_search">
            <div style="padding-left:0" class="main_main_search_item">
              <div class="formItem_header">
                <div class="formItem_header_operate">
                  <a class="formItem_header_operate_items" @click="add">
                    <svg-icon icon-class="新增" />
                    <div class="item_name">新增</div>
                  </a>
                </div>
              </div>
            </div>
            <div class="main_main_search_item">
              <el-input v-model="pageData.searchValue" placeholder="请输入姓名或者手机号" clearable @keyup.enter.native="getList" @clear="getList" />
              <el-button icon="el-icon-search" @click.stop="getList" />
            </div>
          </div>
          <div ref="table_container" class="table_container">
            <el-table
              ref="table"
              :data="tableData"
              :height="tableHeight"
              style="width: 99.9%"
              row-key="menuId"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
              @sort-change="(column)=>{sortChange(column, this)}"
              @row-click="rowClick"
            >
              <el-table-column width="70" align="center">
                <template slot-scope="scope">
                  <!--这里的{{ "" }}为了隐藏索引值 -->
                  <el-radio-group v-model="templateSelection" @click.native="SkipProgress(scope.row,$event)">
                    <el-radio
                      class="radio"
                      :label="scope.row.userId"
                    >{{ '' }}
                    </el-radio>
                  </el-radio-group>
                </template>
              </el-table-column>
              <Column
                v-for="(column,index) in defaultTableColumns"
                :key="index"
                :prop="column.prop"
                :is-show="column.isShow"
                :label="column.label"
                :sortable="column.sortable"
                :width="column.width"
              />
            </el-table>
          </div>

          <div class="item_list">
            <pagination
              :total="total"
              :page.sync="pageData.pageNum"
              :limit.sync="pageData.pageSize"
              :selected-num="selectionsAll.length"
              @pagination="getList"
            />
          </div>
        </div>
      </div>
      <div class="main_bottom">
        <el-button type="cancel" @click="cancel">取消</el-button>
        <el-button @click="onSubmit">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { treeselect as getDeptTree, selectUserByDeptId } from '@/api/system/dept'
import AddOrUpdate2 from '@/views/system/user/addOrUpdate.vue'

export default {
  name: 'Index',
  props: {
    layerid: {},
    bookId: {},
    selectionList: {},
    userName: {},
    userId: {},
    isCarList: {}
  },
  data() {
    return {
      loading: false,
      filterText: '', // 树节点搜索字段
      pageId: '选择申请人', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      currentDeptId: '',
      currentDeptName: '',
      total: 1, // 数据一共有多少条
      tableHeight: '0', // 表格高度
      mainId: 'userId', // 表格行ID
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'realName', label: '姓名', sortable: 'custom', canOrder: false },
        { prop: 'sexName', label: '性别', canOrder: false },
        { prop: 'phonenumber', label: '手机号', canOrder: false },
        { prop: 'email', label: '邮箱', canOrder: false },
        { prop: 'deptName', label: '部门', canOrder: false }
      ],
      equipmentInfoOptions: [],
      activeNum: 1,
      deptData: [],
      templateSelection: undefined,
      selectionsRow: {},
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      rowId: '',
      typeId: ''
    }
  },
  created() {
    this.getDeptTree()
  },
  mounted() {
    this.templateSelection = this.userId
    this.selectionsRow = this.selectionList
    this.$nextTick(() => {
      this.tableHeight = this.$refs.table_container.offsetHeight
    })
  },
  methods: {
    SkipProgress(row, e) {
      if (e.target.tagName === 'INPUT') return
      if (row.userId == this.templateSelection) {
        this.selectionsAll = {}
        this.templateSelection = ''
      } else {
        this.selectionsAll = row
        this.templateSelection = row.userId
      }
    },
    rowClick(row) {
      if (row.userId == this.templateSelection) {
        this.selectionsRow = {}
        this.templateSelection = ''
      } else {
        this.selectionsRow = row
        this.templateSelection = row.userId
      }
    },
    treeNodeClick(data) { // 点击树节点
      this.activeNum = 1
      this.currentDeptName = data.label
      this.currentDeptId = data.id
      this.pageData.pageNum = 1
      Object.keys(this.pageData).forEach(key => { // 给右边数据赋值
        if (key !== 'pageNum' && key !== 'pageSize' && key !== 'orderByColumn' && key !== 'isAsc') {
          this.pageData[key] = ''
        }
      })
      this.getList()
    },
    // 获取部门树
    getDeptTree() {
      getDeptTree().then(res => {
        this.deptData = res.data

        this.$nextTick(() => {
          this.deptOneClick()
        })
      })
    },
    deptOneClick() { // 默认点击一下第一级部门
      document.querySelector('.el-tree-node__content').click()
    },
    // rowClick(row) {
    //   this.selectionsRow = row
    //   this.templateSelection = row.userId
    // },
    getList() {
      const cachePageData = this.$deepClone(this.pageData)
      cachePageData.deptId = this.currentDeptId
      cachePageData.state = 1
      selectUserByDeptId(cachePageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
        })
      })
    },
    // 新增用户
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate2, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
          }// props
        },
        area: ['1320px', '700px'],
        title: '新增用户', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    },
    onSubmit() {
      // if (Object.keys(this.selectionsRow).length == 0 && !this.userId) {
      //   return this.$message.warning(' 请选择申请人')
      // } else if (Object.keys(this.selectionsRow).length == 0 && !this.userId && this.isCarList) {
      //   return this.$message.warning(' 请选择负责人')
      // } else {
      // if (!this.selectionsRow.userName) {
      //   this.selectionsRow.userId = this.templateSelection
      //   this.selectionsRow.realName = this.userName
      // }
      this.$parent.chooseUserInfo(this.selectionsRow)
      this.$layer.close(this.layerid)// 关闭弹框
      // }
    }
  }
}
</script>

<style lang="less" scoped>
.titleText{
  height: 50px;
  line-height: 50px;
  font-size: 16px;
  color: #333333;
  border-bottom: 1px solid #ECECEC;
  margin-bottom:10px;
}
.main_main_search_input{
  margin-top: 20px;
  margin-bottom: 10px ;
  /deep/ .el-input__inner{
    height: 30px;
  }
}
/deep/ .el-input__inner{
  height: 30px !important;
}
.main_main_search_item{
  .el-input{
    width:230px !important;
    margin-right:10px !important;
  }
  .el-button{
    background:#387bfc !important;
    width:30px !important;
  }
}
.table_container{
  margin-top:10px !important;
}
.item_list {
  margin:0 10px 10px 0 !important;
}
.main_main {
  height: calc(100% - 50px) !important;
  margin-right:0 !important;
  margin-left: 0 !important;
  padding-top:0 !important;
}
.main_main_search_item{
  height: calc(100% - 50px) !important;
  .el-input{
    width:200px !important;
    margin-right:10px !important;
  }
  .el-button{
    background:#387bfc !important;
    width:30px !important;
  }
}
.formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}
.main_main_container {
  width: 100%;
  height: calc(100% - 70px);
  display: flex;
  padding-left: 20px !important;
  .main_main_left {
    width: 250px;
    height: calc(100% - 20px);
    padding-right: 20px;
    .title {
      // padding: 10px 0;
      // border-bottom: 1px solid #387BFC;
      margin: 15px 0;
    }
    .main_main_left_top {
      width: 100%;
      height: calc(100% - 80px);
      overflow-y: auto;
      .el-tree {
        background-color: transparent;
      }
      /deep/ .el-tree-node__content:hover {
        background-color: rgba(56, 123, 252, 0.5) !important;
        color: white;
      }
    }
    .main_main_left_bottom {
      width: 100%;
      .main_main_left_bottom_item {
        background-color: rgba(74, 77, 82, 0.2);
        height: 40px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding-left: 24px;
      }
      .main_main_left_bottom_item:last-child {
        border-top: 1px solid white;
      }
      .main_main_left_bottom_item:hover {
        background-color: rgba(56, 123, 252, 0.5);
        color: white;
      }
      .active {
        .main_main_left_bottom_item {
          background-color: rgba(56, 123, 252, 1);
          color: white;
        }
      }
    }
  }
  .main_main_right {
    width: calc(100% - 250px);
    height: 100%;
    display: flex;
    flex-direction: column;
    border-left: 1px solid #666666;

  }
}
.button{
  width: 100px;
  height: 30px;
  color: #fff;
  background-color: #387bfc;
  border-radius: 4px;
  text-align: center;
  line-height: 30px;
}
.formItem_header{
  justify-content: flex-start !important;
  align-items: flex-start !important;
  height: 40px !important;
}
</style>
